import { useMainStore } from '@/store'
import { RouteLocation } from 'vue-router'
import { Ref, ref, watch } from 'vue'

// 获取当前模块
export const getCurrentModule = (route: RouteLocation): {
  module: Ref<Setting.Menu | undefined>;
  croute: Ref<string>;
  current: Ref<Setting.SubMenu | undefined>;
} => {
  const module = ref<Setting.Menu>()
  const croute = ref<string>('')
  const current = ref<Setting.SubMenu>()
  const store = useMainStore()
  const updateMenu = () => {
    for (const k in store.menus) {
      const menu: any = store.menus[k]
      if (k === route.path) {
        croute.value = k
        module.value = menu
        current.value = menu
        break
      }
      menu.routes.forEach((r: any) => {
        if (route.path === r.path) {
          croute.value = k
          module.value = menu
          current.value = r
        }
      })
    }
  }
  updateMenu()
  watch(() => route.path, updateMenu)

  return {
    module,
    croute,
    current
  }
}
